<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <script crossorigin="anonymous" src="/__dist__/botframework-webchat-fluent-theme.production.min.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://esm.sh/@testduet/wait-for",
          "@testing-library/dom": "https://esm.sh/@testing-library/dom"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';
      import { queryAllByTestId } from '@testing-library/dom';

      const {
        testHelpers: { createDirectLineEmulator },
        WebChat: { createDirectLine, testIds }
      } = window;

      run(async function () {
        // We enable reduced motion by default, disable it first.
        await host.sendDevToolsCommand('Emulation.setEmulatedMedia', {
          features: [{ name: 'prefers-reduced-motion', value: '' }]
        });

        const { directLine, store } = createDirectLineEmulator();

        const { isFluentTheme } = renderWebChat({ directLine, store }, document.getElementById('webchat'));

        await pageConditions.uiConnected();

        // WHEN: Upload button is clicked and a ZIP file is selected.
        await host.upload(pageElements.uploadButton(), 'minecraftdungeons.jpg');

        await waitFor(() => expect(queryAllByTestId(document, testIds.sendBoxAttachmentBarItem)).toHaveLength(1));

        if (isFluentTheme) {
          // WHEN: Focus send box and press TAB key to move the focus to the delete button of the first attachment.
          document.querySelector(`[data-testid="${WebChat.testIds.sendBoxTextBox}"]`)?.focus();
          await host.sendTab(1);
        } else {
          // WHEN: Press TAB key twice to move the focus to the delete button.
          await host.sendShiftTab(2);
        }

        // THEN: Should focus on the attachment delete button.
        expect(document.activeElement).toBe(pageElements.allByTestId(testIds.sendBoxAttachmentBarItemDeleteButton)[0]);

        // THEN: Should match screenshot.
        await host.snapshot('local');

        // WHEN: Press ESCAPE key.
        await host.sendKeys('ESCAPE');

        // THEN: Should focus on the send box text box.
        expect(document.activeElement).toBe(pageElements.sendBoxTextBox());

        // THEN: Should match screenshot.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
